<%= form_for @usergroup do |f| %>
  <%= base_errors_for @usergroup %>
  <ul class="nav nav-tabs" data-tabs="tabs">
    <li class="active"><a href="#primary" data-toggle="tab"><%= _("User Group") %></a></li>
    <li><a href="#roles" data-toggle="tab"><%= _("Roles") %></a></li>
    <% if AuthSource.non_internal.present? %>
      <li><a href="#external" data-toggle="tab"><%= _("External Groups") %></a></li>
    <% end %>
    <%= render_tab_header_for(:main_tabs, :subject => @usergroup, :form => f) %>
  </ul>
  <div class="tab-content">
    <div class="tab-pane active" id="primary">
      <% if @usergroup.external_usergroups.any? %>
        <h5>
          <%= _('Only internal (non-LDAP) users can be added manually. LDAP users
                are automatically synced from the External Groups list.') %>
        </h5>
        <h5>
          <%= _('To refresh the list of users, click on the tab "External
                groups" then "Refresh".') %>
        </h5>
        <hr>
      <% end %>
      <%= text_f f, :name %>
      <%= multiple_checkboxes f, :usergroups, @usergroup, Usergroup.except_current(@usergroup),
        { :label => _("User Groups") } %>
      <%= multiple_checkboxes f, :users, @usergroup, User.except_hidden,
        :label => _("Users"), :object_label_method => :select_title %>
    </div>
    <div class="tab-pane" id="roles">
      <%= checkbox_f f, :admin if User.current.can_change_admin_flag? %>
      <%= multiple_checkboxes f, :roles, @usergroup, Role.for_current_user, {:label => _('Roles')} %>
    </div>
    <% if AuthSource.non_internal.present? %>
      <div class="tab-pane" id="external">
        <table class="<%= table_css_classes('table-hover')%>">
          <thead>
            <tr>
              <th><%= s_("Usergroup|Name") %></th>
              <th><%= s_("Usergroup|Auth source") %></th>
              <th><%= _('Actions') %></th>
            </tr>
          </thead>
          <tbody><% @usergroup.external_usergroups.each do |usergroup| %>
              <tr>
                <% next if usergroup.id.nil? %>
                <td><%= h usergroup.name %></td>
                <td><%= h usergroup.auth_source %></td>
                <td><%= display_link_if_authorized(_('Refresh'),
                                                         hash_for_refresh_external_usergroup_path(:id => usergroup),
                                                         :method => :put, :id => usergroup.id,
                                                         :class => "btn btn-default", :title => usergroup.supports_refresh? ? _('Synchronize group from authentication source') : _("Synchronization is not available for #{usergroup.auth_source.name} auth source"),
                                                         :disabled => !usergroup.supports_refresh?) %>
                </td>
              </tr>
            <% end %>
          </tbody>
        </table>
        <button class='btn btn-info' type='button'
          onclick="$('#current_external_usergroups').toggle();"><%= _("Show linked external user groups") %></button>
        <hr>
        <% invalid_external_groups = @usergroup.external_usergroups.any? { |e| e.errors.present? } %>
        <div style='<%= invalid_external_groups ? '' : 'display:none' %>' id='current_external_usergroups'>
          <%= f.fields_for :external_usergroups do |ug| %>
            <%= render 'external', :f => ug %>
          <% end %>
          <hr>
        </div>
        <div class="children_fields">
          <%= new_child_fields_template(f, :external_usergroups, { :partial => 'external' } ) %>
          <%= add_child_link '+ ' + _('Add external user group'), :external_usergroups, { :class => "info", :title => _('link external user group with this user group') } %>
        </div>
      </div>
    <% end %>
    <%= render_tab_content_for(:main_tabs, :subject => @usergroup, :form => f) %>
  </div>
  <%= submit_or_cancel f %>
<% end %>
